﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    public class HashTask : IProcessTask
    {
        private long imageId;
        private string fileName;
        private string hash;
        private DateTime refreshDate;

        public HashTask(long imageId, string fileName, DateTime refreshDate)
        {
            this.imageId = imageId;
            this.fileName = fileName;
            this.refreshDate = refreshDate;
        }

        public void Process()
        {
            this.hash = Utilities.GetHash(this.fileName);
        }

        public void Commit(SQLiteConnection connection)
        {
            var lastRefreshDateParam = new SQLiteParameter();

            SQLiteCommand updateCommand = new SQLiteCommand(connection);
            updateCommand.CommandText = "UPDATE images SET hash = '" + this.hash + "', lastRefreshDate = ? WHERE id = " + this.imageId;
            updateCommand.Parameters.Add(lastRefreshDateParam);

            lastRefreshDateParam.Value = this.refreshDate;
            updateCommand.ExecuteNonQuery();
        }
    }
}
